package com.wenhai.easyexcel.demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.wenhai.easyexcel.entity.ChannelTypeSkuDetail;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class MergeHeadDemo {

    public static void main(String[] args) throws UnsupportedEncodingException {
         write();

    }

    private static void write() {
        // 方法1 注解
        String fileName = MergeHeadDemo.class.getResource("/").getPath() + "mergeWrite" + System.currentTimeMillis() + ".xlsx";

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel.write(fileName).build();
            // 把sheet设置为不需要头 不然会输出sheet的头 这样看起来第一个table 就有2个头了
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").needHead(Boolean.FALSE).build();
            // 这里必须指定需要头，table 会继承sheet的配置，sheet配置了不需要，table 默认也是不需要
            WriteTable writeTable0 = EasyExcel.writerTable(0).head(head("招行","集采")).needHead(Boolean.TRUE).build();
            WriteTable writeTable1 = EasyExcel.writerTable(1).head(head("招行","集采")).needHead(Boolean.FALSE).build();
            WriteTable writeTable2 = EasyExcel.writerTable(2).head(head("中行","积分")).needHead(Boolean.TRUE).build();
            WriteTable writeTable4 = EasyExcel.writerTable(1).head(head("招行","集采")).needHead(Boolean.FALSE).build();
            List<ChannelTypeSkuDetail> data = data();
            // 第一次写入会创建头
            excelWriter.write(data, writeSheet, writeTable0);
            // 第二次写如也会创建头，然后在第一次的后面写入数据
            excelWriter.write(summarizing(data), writeSheet, writeTable1);
            // 第二次写如也会创建头，然后在第一次的后面写入数据
            data = data();
            excelWriter.write(data, writeSheet, writeTable2);
            excelWriter.write(summarizing(data), writeSheet, writeTable4);
        } finally {
            // 千万别忘记finish 会帮忙关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    private static List<ChannelTypeSkuDetail> data() {
        List<ChannelTypeSkuDetail> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ChannelTypeSkuDetail data = new ChannelTypeSkuDetail();
            data.setSkuName("大海");
            data.setPrice(new BigDecimal("100"));
            data.setSkuNum(1);
            data.setTotalAmount(data.getPrice().multiply(new BigDecimal(data.getSkuNum())));

            list.add(data);
        }
        return list;
    }
    private static List<ChannelTypeSkuDetail> summarizing(List<ChannelTypeSkuDetail> channelTypeSkuDetailList) {
        int skuNumSum = channelTypeSkuDetailList.stream().mapToInt(ChannelTypeSkuDetail::getSkuNum).sum();
        BigDecimal totalAmount = channelTypeSkuDetailList.stream().map(ChannelTypeSkuDetail::getTotalAmount).reduce(new BigDecimal(0), BigDecimal::add);

        List<ChannelTypeSkuDetail> list = new ArrayList<>();
        ChannelTypeSkuDetail data = new ChannelTypeSkuDetail();
        data.setSkuName("汇总");
        data.setSkuNum(skuNumSum);
        data.setTotalAmount(totalAmount);
        list.add(data);
        for (int i = 0; i < 3; i++) {
            data = new ChannelTypeSkuDetail();
            list.add(data);
        }
        return list;
    }



    private static List<List<String>> head(String channel, String channelType) {
        List<List<String>> list = new ArrayList<>();
        String channelAndChannelType = channel + channelType;
        List<String> head1 = new ArrayList<>();
        head1.add(channelAndChannelType);
        head1.add("商品名称");
        List<String> head2 = new ArrayList<>();
        head2.add(channelAndChannelType);
        head2.add("单价");
        List<String> head3 = new ArrayList<>();
        head3.add(channelAndChannelType);
        head3.add("数量");
        List<String> head4 = new ArrayList<>();
        head4.add(channelAndChannelType);
        head4.add("总价");
        list.add(head1);
        list.add(head2);
        list.add(head3);
        list.add(head4);
        return list;
    }


}
